﻿<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="Author" content="http://www.dennisbusch.de">
    
    <title>Refugee Lib Example - using rlGL </title>

    <!-- include "Refugee Lib" -->                     
    <script type="text/javascript" src="./script/rlBoot.js"></script> 
    
  </head>
  <body>
    <div id="divMain" style="display: table-cell; width: 400px; height: 300px; vertical-align: middle; text-align: center; color: #FFFFFF; background: #80C0E0;">
    Please enable <i>javascript</i> and then reload this page.
    </div>
  
    <script type="text/javascript" defer>           
      var mainFunc = function(bootCode)
      { 
        console.log("bootCode: "+bootCode);  
        if(bootCode == 0)
        { 
          var width = 400, height = 225;
          var engine = new rlEngine("divMain", "007engine", true, width, height, { antialias: false });
          
          engine.onInputEvent = function(previousEvent, currentEvent)
          {
          
          };
          
          engine.onUpdateLogic = function(LUTick)
          {
          
          };
             
          var vertices = null; // is initialized in loadProgramFinished further down
          
          engine.onUpdateViews = function(GL, G2D, time)
          {
            GL.clearColor(0.5, 0.5, 1.0, 1.0);
            GL.clear(GL.COLOR_BUFFER_BIT);
            G2D.clearRect(0, 0, width, height);
             
            if(vertices != null)
              GL.drawArrays(GL.TRIANGLES, 0, vertices.itemCount);
          };
            
          // prepare webgl program --------------------------------------------- 
          var GL = engine.getGL();
           
          // define callback (will be called by rlGL.loadProgram)
          var loadProgramFinished = function(glp)
          {
            if(glp != null) // program loaded successfully?
            {
              // define vertex data
              var aspectRatio = width / height;
              vertices = new Float32Array([0.0, 0.0, width, 0.0, 0.0, height]);  
              
              // transfer vertex data to GPU
              var vbuf = GL.createBuffer();
              GL.bindBuffer(GL.ARRAY_BUFFER, vbuf);
              GL.bufferData(GL.ARRAY_BUFFER, vertices, GL.STATIC_DRAW);
              
              // store additional data properties
              vertices.itemSize = 2;
              vertices.itemCount = vertices.length / vertices.itemSize;
              
              // init gl program variables
              GL.useProgram(glp);
              glp.uColor = GL.getUniformLocation(glp, "uColor");
              GL.uniform4fv(glp.uColor, [0.0, 1.0, 1.0, 1.00]);
              
              glp.uPixelDimensions = GL.getUniformLocation(glp, "uPixelDimensions");
              GL.uniform2fv(glp.uPixelDimensions, [width, height]);
              
              glp.aVertexPosition = GL.getAttribLocation(glp, "aVertexPosition");
              GL.enableVertexAttribArray(glp.aVertexPosition);
              GL.vertexAttribPointer(glp.aVertexPosition, vertices.itemSize, GL.FLOAT, false, 0, 0);
              
              engine.togglePause(); 
            }
            else
              console.error("failed to load gl program");
          };
          
          // define webgl shader types and sources to load                                 
          var shaderDescriptions = [ "V:./script/shaders/v2DpixelCoords.txt",
                                     "F:./script/shaders/f2DsolidColor.txt" ];
          
          // load webgl shader program                           
          rlGL.loadProgram(GL, shaderDescriptions, loadProgramFinished);
          // -------------------------------------------------------------------
        }
      };
      
      var additionalScripts = [];
      rlBoot.bootRefugeeLib("divMain", additionalScripts, this, mainFunc, true);
    </script>
  </body>
</html>